package com.msb.serviceprice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.msb.internalcommon.constant.CommonStatusEnum;
import com.msb.internalcommon.dto.PriceRule;
import com.msb.internalcommon.dto.ResponseResult;
import com.msb.internalcommon.reponese.DirectionResponse;
import com.msb.internalcommon.reponese.ForecastPriceResponse;
import com.msb.internalcommon.request.ForecastPriceDTO;
import com.msb.internalcommon.util.BigDecimalUtils;
import com.msb.serviceprice.mapper.PriceRuleMapper;
import com.msb.serviceprice.remote.ServiceMapClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: jiudianliu
 * @Date: 2023/6/2 - 06 - 02 - 14:44
 * @Description: com.msb.serviceprice.service
 * @version: 1.0
 */
@Service
@Slf4j
public class ForecastPriceService {


    @Autowired
    private ServiceMapClient serviceMapClient ;

    @Autowired
    private PriceRuleMapper priceRuleMapper ;

    public ResponseResult forecastPrice(String depLongitude, String depLatitude, String destLongitude, String destLatitude,
                                        String cityCode , String vehicleType){

        //log.info("出发地的经度：" + depLongitude);
        //log.info("出发地的纬度：" + depLatitude);
        //log.info("目的地的经度：" + destLongitude);
        //log.info("目的地的经度：" + destLatitude);

        //log.info("调用地图服务，查询距离和时长") ;
        ForecastPriceDTO forecastPriceDTO = new ForecastPriceDTO();
        forecastPriceDTO.setDepLongitude(depLongitude);
        forecastPriceDTO.setDepLatitude(depLatitude);
        forecastPriceDTO.setDestLongitude(destLongitude);
        forecastPriceDTO.setDestLatitude(destLatitude);
        ResponseResult<DirectionResponse> direction = serviceMapClient.direction(forecastPriceDTO);
        Integer distance = direction.getData().getDistance();
        Integer duration = direction.getData().getDuration();
        //log.info("距离：" + distance + ",时长：" + duration);

        //log.info("读取计价规则") ;
        Map<String , Object> queryMap = new HashMap<>();
        queryMap.put("city_code" , cityCode) ;
        queryMap.put("vehicle_type" , vehicleType) ;

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("city_code" , cityCode) ;
        queryWrapper.eq("vehicle_type" , vehicleType) ;
        queryWrapper.orderByDesc("fare_version") ;


        List<PriceRule> priceRules = priceRuleMapper.selectList(queryWrapper);
        if (priceRules.size() == 0) {
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode() , CommonStatusEnum.PRICE_RULE_EMPTY.getValue()) ;
        }


        PriceRule priceRule = priceRules.get(0) ;

        //log.info("根据距离、时长和计价规则，计算价格") ;
        double price = getPrice(distance , duration , priceRule) ;


        ForecastPriceResponse forecastPriceResponse = new ForecastPriceResponse() ;

        forecastPriceResponse.setPrice(price);
        forecastPriceResponse.setCityCode(cityCode);
        forecastPriceResponse.setVehicleType(vehicleType);

        return ResponseResult.success(forecastPriceResponse) ;


    }


    /**
     * 根据距离、时长和计价规则 来计算最终价格
     * @param distance  距离
     * @param duration  时长
     * @param priceRule  计价规则
     * @return
     */
    private static Double getPrice(Integer distance , Integer duration , PriceRule priceRule){
        double price = 0;
        //起步价
        double startFare = priceRule.getStartFare();
        price = BigDecimalUtils.add(price , startFare);

        //里程费
        //总里程  m
        double distanceMile = BigDecimalUtils.divide(distance , 1000) ;
        //起步里程
        double startMile =(double)priceRule.getStartMile();
        double distanceSubtract = BigDecimalUtils.substract(distanceMile,startMile);
        //最终收费的里程数
        Double mile = distanceSubtract<0?0:distanceSubtract ;
        //计程单价  元/km
        double unitPriceMile = priceRule.getUnitPricePerMile() ;
        //里程价格
        double mileFace = BigDecimalUtils.multiply(mile , unitPriceMile);
        price = BigDecimalUtils.add(price,mileFace) ;

        //时长费
        //时长的分钟数
        double timeMinute = BigDecimalUtils.divide(duration , 60);

        //计时单价
        Double unitPricePerMinute = priceRule.getUnitPricePerMinute();

        //时长费用
        double timeFace = BigDecimalUtils.multiply(timeMinute,unitPricePerMinute);
        price = BigDecimalUtils.add(price,timeFace) ;

        BigDecimal priceBigDecimal = BigDecimal.valueOf(price) ;
        priceBigDecimal = priceBigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
        return priceBigDecimal.doubleValue() ;
    }

    public static void main(String[] args) {
        PriceRule priceRule = new PriceRule();
        priceRule.setUnitPricePerMile(1.8);
        priceRule.setUnitPricePerMinute(0.5);
        priceRule.setStartFare(10.0);
        priceRule.setStartMile(3);
        System.out.println(getPrice(6500 , 1800 , priceRule));
    }
}
